package com.sara.service.impl;

import com.sara.entity.Employee;
import com.sara.entity.FixParts;
import com.sara.entity.Order;
import com.sara.entity.OrderEmployee;
import com.sara.exception.ServiceException;
import com.sara.mapper.FixPartsMapper;
import com.sara.mapper.OrderMapper;
import com.sara.service.FixService;
import com.sara.vo.OrderPartsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Wayne
 * @date 2019/1/22
 */
@Service
public class FixServiceImpl implements FixService {

    @Autowired
    private FixPartsMapper fixPartsMapper;

    @Autowired
    private OrderMapper orderMapper;

    private OrderEmployee orderEmployee;
   /* *//**
     * 通过传入的状态，查询符合状态的订单
     *
     * @param state 需要查询的状态
     * @return 符合状态的订单集合
     *//*
    @Override
    public Map<String,Object> findAllOrderWithStates(String state) {

        Map<String, Object> stringObjectMap = fixPartsMapper.selectOrderAndPartsByState(state);
        System.out.println("stringObjectMap" + stringObjectMap);
        return stringObjectMap;



    }*/

    @Override
    public List<Map<String, Object>> findAllOrderWithState(String state) {
        List<Map<String, Object>> stringObjectMap = fixPartsMapper.selectOrderAndPartsByState(state);

        System.out.println("stringObjectMap" + stringObjectMap);
        return stringObjectMap;
    }

    @Override
    public OrderPartsVo findAllOrderWithOrderId(Integer id) {

        OrderPartsVo  orderPartsVo  = fixPartsMapper.selectOrderAndPartsById(id);
        List<FixParts> fixPartsList = fixPartsMapper.selectPartsByOrderId(id);
        orderPartsVo.setPartsList(fixPartsList);
        return orderPartsVo;

    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void editOrderStateByOrderId(Integer id, String state, Employee currEmployee) throws ServiceException {
        Order order = orderMapper.selectByPrimaryKey(id);
        if(order == null){
            throw new ServiceException("订单不存在，或者已经被处理");
        }
        if(order.getState().equals(state)){
            throw new ServiceException("操作异常。。。");
        }

        if(Order.ORDER_STATE_FIXING.equals(state)){
            orderEmployee.setEmployeeId(currEmployee.getId());
            orderEmployee.setOrderId(id);
        }else if (Order.ORDER_STATE_CHECKING.equals(state)){
            orderEmployee.setEmployeeId(currEmployee.getId());
            orderEmployee.setEmployeeId(id);
        }

        order.setState(state);
        orderMapper.updateByPrimaryKeySelective(order);

    }
}
